package guoan.service;

import guoan.core.common.WebConfig;
import guoan.domain.RandomKey;
import guoan.repository.RandomKeyRepository;

import java.util.List;

import javax.transaction.Transactional;

import org.apache.commons.lang3.RandomStringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Transactional
public class RandomKeyService  {
	protected final Logger log = LoggerFactory.getLogger(getClass());

	@Autowired
	private RandomKeyRepository randomKeyRepository;

	public List<RandomKey> findByTrainIdAndCoachTypeAndIsUse(Long trainId, Long coachType, long l) {
		// TODO Auto-generated method stub

		Long size = randomKeyRepository.countByTrainIdAndCoachTypeAndIsUse(trainId, coachType, l);

		// 如果位数不够则新增
		if (size.intValue() < 50) {
			int rows = WebConfig.EXAM_COACH_MAX - size.intValue();
			rAdd(trainId, coachType, rows);
		}
		return randomKeyRepository.findByTrainIdAndCoachTypeAndIsUse(trainId, coachType, l);
	}

	public void rAdd(Long trainId, Long coachType, int max) {
		for (int i = 0; i < max; i++) {
			addRandomKey(trainId, coachType);
		}
	}

	public boolean addRandomKey(Long trainId, Long coachType) {
		boolean isAdd = false;
		int rn = coachType.intValue()==1?12:10;
		while (!isAdd) {
            String random = RandomStringUtils.randomAlphanumeric(rn).toUpperCase();
			if (randomKeyRepository.countByKeyAndIsUse(random, 0L) == 0) {
				RandomKey randomKey = new RandomKey();
				randomKey.setCoachType(coachType);
				randomKey.setIsUse(0L);
				randomKey.setTrainId(trainId);
				randomKey.setKey(random);
				if (randomKeyRepository.save(randomKey).getId() != null) {
					isAdd = true;
				}
			}
		}
		return isAdd;
	}
	
	public String addRandomKey(Long trainId, Long coachType,Long isUse) {
        boolean isAdd = false;
        String k = "";
        while (!isAdd) {
            String random = RandomStringUtils.randomAlphanumeric(10).toUpperCase();
            if (randomKeyRepository.countByKeyAndIsUse(random, 0L) == 0) {
                RandomKey randomKey = new RandomKey();
                randomKey.setCoachType(coachType);
                randomKey.setIsUse(isUse);
                randomKey.setTrainId(trainId);
                randomKey.setKey(random);
                if (randomKeyRepository.save(randomKey).getId() != null) {
                    isAdd = true;
                    k = random;
                }
            }
        }
        return k;
    }

	public List<RandomKey> findByStudentId(Long studentId) {
		// TODO Auto-generated method stub
		return randomKeyRepository.findByStudentId(studentId);
	}

	public RandomKey findByStudentIdAndType(Long studentId, long coachType) {
		// TODO Auto-generated method stub
		return randomKeyRepository.findByStudentIdAndCoachType(studentId, coachType);
	}

	public void updateByStudentIdAndCoachType(Long studentId, long coachType) {
		// TODO Auto-generated method stub
		randomKeyRepository.updateByStudentIdAndCoachType(studentId, coachType);
	}

	public RandomKey findByKeyAndIsUse(String upperCase, long l) {
		// TODO Auto-generated method stub
		return randomKeyRepository.findByKeyAndIsUse(upperCase, l);
	}

	public void save(RandomKey randomKey) {
		// TODO Auto-generated method stub
		randomKeyRepository.save(randomKey);
	}

	public RandomKey findByKey(String upperCase) {
		// TODO Auto-generated method stub
		return randomKeyRepository.findByKey(upperCase);
	}

    public RandomKey findOne(Long id){
        // TODO Auto-generated method stub
        return randomKeyRepository.findOne(id);
    }

}
